#!/usr/bin/env node

/*
 * showcert - show X.509 binary or PEM certificate
 *
 * Copyright (c) 2015-2016 Kenji Urushima (kenji.urushima@gmail.com)
 *
 * This software is licensed under the terms of the MIT License.
 * https://kjur.github.io/jsrsasign/license
 *
 * The above copyright and license notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * Please use '-h' option for this script usage.
 * ---------------------------------------------------------
 * DESCRIPTION
 *   This script shows DER binary or PEM X.509 certificate.
 *
 * USAGE
 *   % showcert comodo_ee1.cer
 *   Basic Fields
 *     serial number: 00d09282634303a97fadf55568a48ca87e
 *     ... snip ...
 *   X509v3 Extensions:
 *     authorityKeyIdentifier :
 *     ... snip ...
 *   signature algorithm: SHA256withRSA
 *   signature: 64228b84fd2f3bcf...
 */

var program = require('commander');
var rs = require('jsrsasign');
var rsu = require('jsrsasign-util');

program
    .version('0.0.2 (2016-Sep-11)')
    .usage('[options] <DER binary or PEM certificate file>')
    .description('show X.509 binary or PEM certificate')
    .parse(process.argv);

if (program.args.length !== 1)
    throw "wrong number of arguments";

var file = program.args[0];

var data = rsu.readFile(file);
var pem;
if (data.substr(0, 5) === "-----") {
    pem = data;
} else {
    var hex = rs.rstrtohex(data);
    pem = rs.asn1.ASN1Util.getPEMStringFromHex(hex, "CERTIFICATE");
}

var x = new rs.X509();
x.readCertPEM(pem);
console.log(x.getInfo());

